package com.xingfly.repository;

import com.xingfly.model.Following;
import com.xingfly.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * Created by SuperS on 2016/11/29.
 */
public interface FollowingRepository extends JpaRepository<Following, Long> {
    //根据userId获取用户的关注列表
    List<Following> findByUserId(Long id);

    //根据用户ID和关注ID 获取关注关系对象
    @Query("SELECT f FROM Following f WHERE f.user.id=:userId AND f.followingUser.id=:followingUserId")
    Following findByUserIdAndFollowingUserId(@Param("userId") Long userId, @Param("followingUserId") Long followingUserId);

    //使用JPQL 通过用户ID获取 关注 数量
    @Query("SELECT COUNT(f) FROM Following f WHERE f.user.id=:id")
    Long getFollowingCountByUserId(@Param("id") Long userId);

    //使用JPQL通过用户ID 获取关注列表
    @Query("SELECT f.followingUser FROM Following f WHERE f.user.id=:id")
    List<User> findFollowingsByUserId(@Param("id") Long userId);

    //使用JPQL通过用户ID 获取关注列表ID
    @Query("SELECT f.followingUser.id FROM Following f WHERE f.user.id=:id")
    List<Long> getFollowingIdsByUserId(@Param("id") Long userId);

    @Query("SELECT f FROM Following f WHERE f.user.id=:id AND f.followingUser.id IN :ids")
    List<Following> isFollowingUsers(@Param("id") Long userId, @Param("ids") List<Long> users_id);
}
